package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sso.pojo.dto.OauthParamAddDTO;
import com.xbongbong.sso.pojo.dto.OauthParamDeleteDTO;
import com.xbongbong.sso.pojo.dto.OauthParamDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthParamListDTO;
import com.xbongbong.sso.pojo.dto.OauthParamUpdateDTO;
import com.xbongbong.sso.pojo.vo.OauthParamAddVO;
import com.xbongbong.sso.pojo.vo.OauthParamDetailVO;
import com.xbongbong.sso.pojo.vo.OauthParamListVO;
import com.xbongbong.sso.pojo.vo.OauthParamUpdateVO;
import com.xbongbong.sso.service.OauthParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 授权参数
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/oauthParam")
public class OauthParamController {

    private static final Logger LOG = LoggerFactory.getLogger(OauthParamController.class);

    @Resource
    private OauthParamService oauthParamService;

    /**
     * 授权参数列表
     * @param oauthParamListDTO 关联DTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid OauthParamListDTO oauthParamListDTO, BindingResult br) throws Exception{
        XbbResponse<OauthParamListVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthParamListVO oauthParamListVO = oauthParamService.list(oauthParamListDTO);
                response = new XbbResponse<>(oauthParamListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权参数新增
     * @param oauthParamAddDTO 关联DTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid OauthParamAddDTO oauthParamAddDTO, BindingResult br) throws Exception{
        XbbResponse<OauthParamAddVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthParamAddVO oauthParamAddVO = oauthParamService.add(oauthParamAddDTO);
                response = new XbbResponse<>(oauthParamAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权参数详情
     * @param oauthParamDetailDTO 关联DTO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid OauthParamDetailDTO oauthParamDetailDTO, BindingResult br) throws Exception{
        XbbResponse<OauthParamDetailVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthParamDetailVO oauthParamDetailVO = oauthParamService.detail(oauthParamDetailDTO);
                response = new XbbResponse<>(oauthParamDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权参数更新
     * @param oauthParamUpdateDTO 关联DTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid OauthParamUpdateDTO oauthParamUpdateDTO, BindingResult br) throws Exception{
        XbbResponse<OauthParamUpdateVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthParamUpdateVO oauthParamUpdateVO = oauthParamService.update(oauthParamUpdateDTO);
                response = new XbbResponse<>(oauthParamUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 授权参数删除
     * @param oauthParamDeleteDTO 关联DTO
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid OauthParamDeleteDTO oauthParamDeleteDTO, BindingResult br) throws Exception{
        XbbResponse<BaseVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = oauthParamService.delete(oauthParamDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
